<template>
  <div class="notification-management">
    <!-- 查询筛选区域 -->
    <el-card class="query-card" shadow="never">
      <el-form :model="queryParams" inline>
        <el-form-item label="接收者ID">
          <el-input v-model="queryParams.receiverId" placeholder="请输入接收者ID" clearable />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="queryParams.status" placeholder="请选择状态" clearable style="width: 150px">
            <el-option label="全部" value="" />
            <el-option label="未读" value="0" />
            <el-option label="已读" value="1" />
            <el-option label="已删除" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 操作栏 -->
    <el-card class="action-card" shadow="never">
      <el-button type="danger" icon="Delete" @click="batchDeleteNotifications">批量删除</el-button>
      <span style="margin-left: 20px;">共 <b>{{ total }}</b> 条通知</span>
    </el-card>

    <!-- 通知表格 -->
    <el-card class="content-card" shadow="never">
      <el-table
        ref="multipleTableRef"
        :data="tableData"
        border
        stripe
        v-loading="loading"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="receiverId" label="接收者ID" align="center" />
        <el-table-column prop="senderId" label="发送者ID" align="center">
          <template #default="{ row }">
            <span v-if="row.senderId === 0">系统</span>
            <span v-else>{{ row.senderId }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="content" label="通知内容" align="center" width="350" />
        <el-table-column prop="sendTime" label="发送时间" align="center" />
        <el-table-column prop="readTime" label="阅读时间" align="center">
          <template #default="{ row }">
            <span v-if="row.readTime">{{ row.readTime }}</span>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" align="center">
          <template #default="{ row }">
            <el-tag :type="row.status === 0 ? 'warning' : row.status === 1 ? 'success' : 'info'">
              {{ row.status === 0 ? '未读' : row.status === 1 ? '已读' : '已删除' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="150">
          <template #default="{ row }">
            <el-button text type="primary" size="small" @click="viewDetail(row)">详情</el-button>
            <el-button text type="danger" size="small" @click="deleteNotification(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        v-model:current-page="queryParams.current"
        :page-size="queryParams.size"
        :page-sizes="[5, 7, 9, 10]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="fetchData"
        style="margin-top: 16px; text-align: right"
      />
    </el-card>

    <!-- 详情弹窗 -->
    <el-dialog v-model="detailDialogVisible" title="通知详情" width="500px">
      <el-form label-width="100px">
        <el-form-item label="接收者ID">
          <span class="dialog-content">{{ currentRow.receiverId }}</span>
        </el-form-item>
        <el-form-item label="发送者ID">
          <span class="dialog-content">
            {{ currentRow.senderId === 0 ? '系统' : currentRow.senderId }}
          </span>
        </el-form-item>
        <el-form-item label="通知内容">
          <span class="dialog-content">{{ currentRow.content }}</span>
        </el-form-item>
        <el-form-item label="发送时间">
          <span class="dialog-content">{{ currentRow.sendTime }}</span>
        </el-form-item>
        <el-form-item label="阅读时间">
          <span class="dialog-content">
            {{ currentRow.readTime || '--' }}
          </span>
        </el-form-item>
        <el-form-item label="状态">
          <el-tag :type="currentRow.status === 0 ? 'warning' : currentRow.status === 1 ? 'success' : 'info'">
            {{ currentRow.status === 0 ? '未读' : currentRow.status === 1 ? '已读' : '已删除' }}
          </el-tag>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="detailDialogVisible = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { notificationApi } from '@/api/notification'

// 查询参数
const queryParams = reactive({
  receiverId: '',
  status: '',
  current: 1,
  size: 5
})

// 表格数据相关
const tableData = ref([])
const total = ref(0)
const loading = ref(false)

// 多选引用
const multipleTableRef = ref(null)
const multipleSelection = ref([])

// 弹窗相关
const detailDialogVisible = ref(false)
const currentRow = ref({})

// 获取数据
const fetchData = async () => {
  loading.value = true
  try {
    const response = await notificationApi.page(
      queryParams.receiverId,
      queryParams.current,
      queryParams.size,
      queryParams.status
    )
    tableData.value = response.records
    total.value = response.total
  } catch (e) {
    ElMessage.error('获取通知失败')
  } finally {
    loading.value = false
  }
}

// 查询
const handleQuery = () => {
  queryParams.current = 1
  fetchData()
}

// 重置
const resetQuery = () => {
  queryParams.receiverId = ''
  queryParams.status = ''
  fetchData()
}

// 查看详情
const viewDetail = (row) => {
  currentRow.value = { ...row }
  detailDialogVisible.value = true
}

// 删除通知
const deleteNotification = (id) => {
  ElMessageBox.confirm('确定要删除该通知吗？', '提示', { type: 'warning' })
    .then(async () => {
      await notificationApi.deleteById(id)
      ElMessage.success('删除成功')
      fetchData()
    })
    .catch(() => {})
}

// 批量删除
const batchDeleteNotifications = () => {
  const selectedIds = multipleSelection.value.map(item => item.id)
  if (selectedIds.length === 0) {
    ElMessage.warning('请至少选择一条通知')
    return
  }

  ElMessageBox.confirm('确定要删除选中的通知吗？', '提示', { type: 'warning' })
    .then(async () => {
      await notificationApi.batchDelete(selectedIds)
      ElMessage.success('批量删除成功')
      fetchData()
    })
    .catch(() => {ElMessage.error(e.message || '删除失败')})
}

// 处理多选
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}

// 切换每页条数
const handleSizeChange = (newSize) => {
  queryParams.size = newSize
  fetchData()
}

import { onMounted } from 'vue'
onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.notification-management {
  padding: 16px;
  background: #f5f7fa;
  min-height: 100vh;
}
.query-card,
.action-card,
.content-card {
  margin-bottom: 16px;
}
.dialog-content {
  display: block;
  padding: 10px 15px;
  background: #f9f9f9;
  border-radius: 4px;
}
</style>